package controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.CTPhieuNhap;
import model.PhieuNhap;

public class PhieuNhapController {
	public static List<PhieuNhap> getCtpn(int idncc) {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM hdphieunhap WHERE idncc=" + idncc + "";
			ResultSet rs = stmt.executeQuery(sql);
			PhieuNhap pn;
			List<PhieuNhap> list = new ArrayList<>();
			while (rs.next()) {
				pn = new PhieuNhap();
				pn.setMaHDPN(rs.getInt("maHDPN"));
				pn.setNgayNhap(rs.getDate("ngayNhap"));
				NhaCCController d = new NhaCCController();
				pn.setnCC(d.getNhaCC(idncc));
				CTPhieuNhapController ct = new CTPhieuNhapController();
				List<CTPhieuNhap> ctpn = ct.getCtpn(rs.getInt("maHDPN"));
				pn.setCtpn(ctpn);
				list.add(pn);

			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static void main(String[] args) {
		PhieuNhapController n = new PhieuNhapController();
		System.out.println(n.getCtpn(1));
	}
}
